//
//  WaitPayViewController.m
//  FKGW
//
//  Created by song ce on 2017/2/20.
//  Copyright © 2017年 song ce. All rights reserved.
//

#import "WaitPayViewController.h"
#import "WatipayCell.h"
#import "WaitPayHeadView.h"
#import "WaitPayFooterView.h"
@interface WaitPayViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)NSMutableArray *dataSource;


@end

@implementation WaitPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray array];
    [self tableViewAndHeaderView];
    if(!ISNOTNEEDMESSAGEBTN){
        self.isNeedMessageItem = YES;
    }
    [self loadData];

}

-(void)loadData{
    //2.34查看我的待付款订单接口
    NSDictionary *paraDic = @{
                              @"userId":USERID
                              };
    __weak typeof(self)  weakSelf = self;
    [MBProgressHUD showLoadingGif];
    [Tools AFNetworkingRequestTypePostWithUrl:GETMYUNPAY paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
        DLog(@"待付款订单---%@",resultDic);
        if([resultDic[@"flag"] isEqualToString:@"true"]){
            NSArray *data = resultDic[@"data"];
            if([data isKindOfClass:[NSNull class]]){
                [MBProgressHUD dismissHUD];
            }
            for(NSDictionary *dic  in data){
                WaitPayGroup *group = [[WaitPayGroup alloc]init];
                group.storeId = dic[@"storeId"];
                group.storeName = dic[@"storeName"];
                group.oId = dic[@"oId"];
                NSArray *products= dic[@"products"];
                NSMutableArray *waitPayModels = [NSMutableArray array];
                for(NSDictionary *pDic in products){
                    WatitPayModel *model = [[WatitPayModel alloc]init];
                    model.pPic =pDic[@"pPic"];
                    model.pName =pDic[@"pName"];
                    model.pPrice =pDic[@"pPrice"];
                    model.pProperty =pDic[@"pProperty"];
                    model.pNum =pDic[@"pNum"];
                    [waitPayModels addObject:model];
                }
                [weakSelf.dataSource addObject:group];
            }
            if(!weakSelf.dataSource.count){
                [MBProgressHUD dismissHUD];
                [weakSelf.view addSubview:weakSelf.noDataIUmageV];
                return ;
            }
            [weakSelf.tableV reloadData];
            weakSelf.tableV.hidden = NO;
            [MBProgressHUD dismissHUD];
        }else{
            [MBProgressHUD dismissHUD];
            if(resultDic[@"errorString"]){
                [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
            }
        }
    }];
}

- (void)tableViewAndHeaderView{
    //表视图
    _tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviBarHeight, Main_Screen_Width, Main_Screen_Height-kNaviBarHeight) style:UITableViewStyleGrouped];
    _tableV.showsVerticalScrollIndicator = NO;
    _tableV.backgroundColor = DEF_COLOR242;
    _tableV.delegate = self;
    _tableV.dataSource =self;
    _tableV.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableV];
    _tableV.tableFooterView = [[UIView alloc]init];
    _tableV.hidden = YES;
}

//2.35待付款订单删除
-(void)cancelWaitPayOrderWithOld:(NSString*)old{
   }
#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    WaitPayGroup *group = self.dataSource[section];
    return group.waitPayModels.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *ident=@"actell";
    WatipayCell *cell=[tableView dequeueReusableCellWithIdentifier:ident];
    if(cell==nil){
        cell=[[[NSBundle mainBundle]loadNibNamed:@"WatipayCell" owner:self options:nil]lastObject];
    }
    WaitPayGroup *group = self.dataSource[indexPath.section];
    WatitPayModel *model = group.waitPayModels[indexPath.row];
      cell.model = model;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 101;
}
//自定义头视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSString *headID = @"head";
    WaitPayHeadView *headView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headID];
    if(headView==nil){
        headView = [[[NSBundle mainBundle]loadNibNamed:@"WaitPayHeadView" owner:self options:nil]lastObject];
    }
    headView.group = self.dataSource[section];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 32;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSString *headID = @"footer";
    WaitPayFooterView *footerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:headID];
    if(footerView==nil){
        footerView = [[[NSBundle mainBundle]loadNibNamed:@"WaitPayFooterView" owner:self options:nil]lastObject];
    }
    WaitPayGroup *group =self.dataSource[section];
    footerView.group =group;
    //删除
    footerView.cancelWaitPayBlock= ^(){
        NSDictionary *paraDic = @{
                                  @"userId":USERID,
                                  @"orderId":group.oId
                                  };
//        __weak typeof(self)  weakSelf = self;
        [Tools AFNetworkingRequestTypePostWithUrl:DELETEORDER paramters:paraDic  afterResultBlock:^(NSDictionary *resultDic) {
            if([resultDic[@"flag"] isEqualToString:@"true"]){
                NSDictionary *dataDic = resultDic[@"data"];
                if([dataDic isKindOfClass:[NSNull class]]) return ;
                if([dataDic[@"'status"] isEqualToString:@"true"]){
                    [MBProgressHUD showTipMessageInView:@"'删除成功"];
                    //回到主线程刷新UI
                    dispatch_async(dispatch_get_main_queue(), ^{
                        if (self.dataSource.count) {
                            [self.dataSource removeObjectAtIndex:section];
                            if (section == self.dataSource.count) {
                                // 删除的是最后一个直接刷新
                                [self.tableV reloadData];
                                self.tableV.hidden = YES;
                            }else{
                                [self.tableV deleteSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
                                [self.tableV reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationNone];
                            }
                        }
                    });
                }else{
                    if(resultDic[@"errorString"]){
                        [MBProgressHUD showTipMessageInWindow:resultDic[@"errorString"]];
                    }
                }
            }
        }];
    };
    return footerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 94;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
